﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CrmManageSystemClient.Formn
{
    public partial class FmLogin : Form
    {
        public FmLogin()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            logining();
        }

        private void Login_Load(object sender, EventArgs e)
        {

            //MessageBox.Show(Kernel.Encrypt.Aes.AESEncrypt("舒月祥"));
            
            
            try
            {
                //设置图标
                this.Icon = Resources.ResourceFavicon.Lgoin;
                Config.Xml.CheckConfig();
                Config.Model.Login login = new Config.Model.Login();
                login = Config.Xml.LoginConfigRead();
                textBox1.Text = login.DataAddress;
                textBox2.Text = login.DataAccounts;
                textBox3.Text = login.DataPassword;
                if (login.SavePassword == 1)
                {
                    checkBox1.Checked = true;
                }
                else
                {
                    checkBox1.Checked = false;
                }

                if (login.AutoLogon == 1)
                {
                    checkBox2.Checked = true;
                    logining();
                }
                else
                {
                    checkBox2.Checked = false;
                }
                textBox1.Text = Kernel.Encrypt.Aes.AESEncrypt("舒月祥");
                //textBox1.Text = Kernel.Encrypt.Aes.AESDecrypt("kqVOYjt73i3443hHoaT4xQ==");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误:");
            }
        }

        /// <summary>
        /// 登陆
        /// </summary>
        private void logining()
        {
            try
            {
                Data.DataCommon.SqlString = Data.DataCommon.CreateSqlString(textBox1.Text, "Cqjh2012", textBox2.Text, textBox3.Text, false);
                Data.DataCommon.Landing(Data.DataCommon.SqlString);
                Config.Model.Login login = new Config.Model.Login();
                login.DataAddress = textBox1.Text;
                login.DataAccounts = textBox2.Text;
                
                if (checkBox1.Checked)
                {
                    login.SavePassword = 1;
                    login.DataPassword = textBox3.Text;
                }
                else
                {
                    login.SavePassword = 0;
                }

                if (checkBox2.Checked)
                {
                    login.AutoLogon = 1;
                }
                else
                {
                    login.AutoLogon = 0;
                }
                //Data.DataGrasp DataGrasp = new Data.DataGrasp();

                Data.DataGrasp.Get_Cache();
                Config.Xml.LoginConfig(login);
                Config.Xml.LogConfigRead();
                Log.LogClass.LogWrite("登陆系统!");
                DialogResult = DialogResult.OK;
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "登陆失败:");
            }

        }
    }
}
